<?php
/**
 * Object Relational Persistence Mapping Class for table: composite_service_item
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class CompositeServiceAsso extends ORDataObject {

	/**#@+
	 * Fields of table: composite_service_item mapped to class members
	 */
	var $composite_service_asso_id	= 	'';
	var $composite_service_id		= 	'';
	var $service_id					= 	'';
	var $optional 					=  	'';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'composite_service_asso';

	/**
	 * Primary Key
	 */
	var $_key = 'composite_service_asso_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'CompositeServiceAsso';

	/**
	 * Handle instantiation
	 */
	function CompositeServiceAsso() {
		parent::ORDataObject();
	}

	public function getServiceInACompositeService($compositeServiceId) {
		$ds = new Datasource_sql();
		$sql = array(
			'cols'	=> "service_id",
			'from'	=> "$this->_table",
			'where'	=> "composite_service_id = $compositeServiceId"			
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function getOptional($serviceId){
		$ds = new Datasource_sql();
		$sql = array (
			'cols'	=> "optional",
			'from'	=> $this->_table,
			'where' => "service_id = $serviceId"
		);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray('optional');
		return $result[0];			
	}
		
	public function delete($ticketItemId) {
		$sql = "delete from $this->_table where ticket_item_id = $ticketItemId";
		$this->_execute($sql);
	}		
	
	public function addItem($ticketItemId, $testItem) {
			
		foreach ($testItem as $serviceId) {
			$data = array (
				'ticket_item_id' 	=> $ticketItemId,
				'service_id'		=> $serviceId);
			$or = Celini::newORDO('CompositeServiceItem');
			$or->populateArray($data);			
			$or->persist();
		}
	}
	
	public function listCompositeService($compositeId) {	    
		$field		=	L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		
		$ds = new Datasource_sql();
		$sql = array(
			'cols'	=> "S.service_id, S.$field name, CSA.optional, S.price",
			'from'	=> "composite_service_asso CSA 
				INNER JOIN service S ON CSA.service_id = S.service_id",
			'where'	=> "CSA.composite_service_id = $compositeId",
			'orderby'	=> "CSA.optional desc"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
    function getDefaultServiceByCompositeService($compositeId) {
        $field      =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
    	
    	$ds = new Datasource_sql();
        $sql = array(
            'cols'    => "s.service_id, s.$field name, a.optional",
            'from'    => "composite_service_asso a 
                    INNER JOIN service s ON (s.service_id = a.service_id)",
            'where'   => "a.composite_service_id = $compositeId and optional = 'no' " ,
            'orderby' => "s.service_id"
            );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
    }

	function deleteServiceOfCS($csId) {
    	$sql = "DELETE FROM $this->_table WHERE composite_service_id = $csId";
        $this->_execute($sql);        
    }
}
?>
